import { Component, OnInit, Injector } from '@angular/core';
import { CityListComponent } from '../../school/cityList';
import { NzMessageService, NzModalService } from 'ng-zorro-antd';
import { HttpClient } from "@angular/common/http";
import { Router, ActivatedRoute, Params } from '@angular/router';
import { NgZorroPageBaseComponent } from '../../../core/ng-zorro-page-base-component/ng-zorro-page-base-component.component';
import {
  PagedResultDto, ClassInfoOutput, PagerSearchInput, HttpService, BaseComponentConfig,
  CustomNavigation, KeyValueOutput, SearchValue
} from '../../..';
@Component({
  selector: 'app-teacher-student-edit',
  templateUrl: './teacher-student-edit.component.html',
  styleUrls: ['./teacher-student-edit.component.scss']
})
export class TeacherStudentEditComponent extends NgZorroPageBaseComponent implements OnInit {
  studentId;
  remark; // 备注
  project = []; // 项目
  departmentName; // 系
  allstudentdata;
  listEntitiesdata = [];
  aclear;
  listprodata;
  classproject = [];
  projectIdarr = [];
  lstCheckedId = [];
  projectadd = [];

  username;
  password;
  studentname;
  studentphone;
  studentnum;
  className;
  studentuparr = [];
  ngOnInit() {
    this.activatedRoute.queryParams.subscribe(
      data => this.studentId = (data['studentId']));
    if (this.studentId !== "add") {
      this.http1.get<any>(`TeacherInfo/GetStduentInfoByclassInfo?uid=${this.studentId}`).then(e => {
        this.className = e.classInfoId;
        this.username = e.userName;
        this.password = e.userPwd;
        this.studentname = e.personName;
        this.studentphone = e.phoneNumber;
        this.studentnum = e.personNo;



        if (this.projectIdarr[0]) {
          this.projectIdarr.forEach(prolist => {
            this.lstCheckedId.push(prolist);
          });
        }

      });
    }

    this.IniObserver.subscribe(sb => {
      this.getEntityList();
    });



  }
  constructor(injecter: Injector,
    public http1: HttpService,
    public http: HttpClient,
    private activatedRoute: ActivatedRoute,
  ) {
    super(injecter,
      new BaseComponentConfig(
        "TeacherInfo/GetAllClassInfoByTeacher",
        "",
        "",
        "",
        "",
        "id",
        "",
        "学生管理",
        [],
        "",
        { Edit: false, Dell: false },
      ));


  }



  async serve(type: string) {
    this.studentuparr = [];
    const that = this;
    const data = {
      "userName": this.username, "userPwd": this.password, "classInfoId": this.className,
      "personName": this.studentname
    };
    if (this.studentId !== "add") {
      data["id"] = that.studentId;
    }
    Object.keys(data).forEach(function (trait) {
      that.studentuparr.push(data[trait]);
    });
    let i = 0;
    this.studentuparr.forEach(element => {
      if (element === undefined || element === "") {

      } else {
        i++;
      }
    });

    if (i === this.studentuparr.length) {
      data["phoneNumber"] = this.studentphone;
      data["personNo"] = this.studentnum;
      if (this.studentId !== "add") {
        await that.http1.post<any>("TeacherInfo/UpdateClassInfoStudent", data);
      } else {
        await that.http1.post<any>("TeacherInfo/CreateClassInfoStudent", data);
      }
    } else {
      that.message.info('* 为必填选项');
      return false;
    }

    this.message.create(type, `保存成功`);

    this.router.navigate(['app/teacherstudentindex']);

  }

  delete() {

  }
  async provinceChangesol(e) {
    this.departmentName = e;
  }
  async projectdata(value: string[]) {
    this.projectadd = value;

  }


  async searchChange(searchText: string) {
    const that = this;
    clearTimeout(this.aclear);
    this.aclear = setTimeout(() => {
      that.sealist(searchText);
    }, 1000);


  }


  sealist(i) {
    const datalist = { "Name": i };
    this.listEntitiesdata = [];
    this.http1.post<any>("School/GetSchoolToTen", datalist).then(e => {
      this.listEntitiesdata = e;

    });
  }

  classpd(id) {
    if (this.lstCheckedId.indexOf(id) < 0) {
      this.lstCheckedId.push(id);
    } else {
      this.lstCheckedId = this.lstCheckedId.filter(e => e !== id);
    }
  }
}

